﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AlterGear.SQLLibrary;
 
namespace AlterGear.SqlIndexerUI
{
    public partial class FrmSQLConnect : Form
    {
        private List<string> lstSQLInstances;
        //private SQLList sqlList;
        public SQLList sqlList { get; set; }

        public FrmSQLConnect()
        {
            InitializeComponent();
            sqlList = new SQLList();
        }
        public FrmSQLConnect(SQLList _sqllist)
        {
            InitializeComponent();
            sqlList = _sqllist;
            System.Data.SqlClient.SqlConnectionStringBuilder sqlConnBuilder = sqlList.GetSqlConnectionStringBuilder();

            this.comboSQLServer.Text = sqlConnBuilder.DataSource;
            this.chkBoxIntegratedSecurity.Checked = sqlConnBuilder.IntegratedSecurity;
            this.txtUsername.Text = sqlConnBuilder.UserID;
            this.txtPassword.Text = sqlConnBuilder.Password;
            this.comboDB.Text = sqlConnBuilder.InitialCatalog;
        }

        private void comboSQLServer_DropDown(object sender, EventArgs e)
        {
            if (this.lstSQLInstances == null)
            {
                this.lstSQLInstances = SQLList.GetSqlInstances();
                this.comboSQLServer.DataSource = this.lstSQLInstances;
            }
        }

        private void comboDB_DropDown(object sender, EventArgs e)
        {
            List<string> lstDatabases = new List<string>();

            if (String.IsNullOrEmpty(this.comboSQLServer.Text.Trim()) == true)
            {
                MessageBox.Show("Select an SQL Instance", "SQL  instance not selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                    lstDatabases = SQLList.GetSQLDatabase(this.comboSQLServer.Text.ToString(),
                        this.chkBoxIntegratedSecurity.Checked,
                        this.txtUsername.Text, this.txtPassword.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "SQL Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            this.comboDB.DataSource = lstDatabases;
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (this.CanConnect())
            {
                MessageBox.Show("Connection succeeded", "Connection succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool CanConnect()
        {
            bool canConnect = false;
            Exception exception = null;
            string connectionString = "";
            if (string.IsNullOrEmpty(this.comboSQLServer.Text) == true)
            {
                MessageBox.Show("Select an SQL Instance", "SQL  instance not selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (this.comboDB.Text == null)
            {
                MessageBox.Show("Select a database", "SQL database could not be connected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                canConnect = this.sqlList.CanConnect(this.comboSQLServer.Text.ToString(),
                      this.chkBoxIntegratedSecurity.Checked,
                      this.txtUsername.Text, this.txtPassword.Text,
                      this.comboDB.Text.ToString(), (int)this.numericTimeout.Value, false, 
                      out exception, out connectionString);
                if (canConnect == false)
                {
                    MessageBox.Show(exception.Message, "SQL Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
            return canConnect;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.CanConnect())
            {
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
